package com.wc.算法提高课.C第三章_图论.最小生成树.连接格点;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/16 19:32
 * @description https://www.acwing.com/problem/content/1146/
 */
public class Main {
    /**
     * 思路：kruskal
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, M = N * N;
    static int[] p = new int[M];
    static PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(o -> o[2]));
    static int[][] g = new int[N][N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1, t = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                g[i][j] = t++;
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int a = g[i][j], b = g[i + 1][j], c = 1;
                if(i + 1 <= n)
                    q.add(new int[]{a, b, c});
                b = g[i][j + 1];
                c = 2;
                if(j + 1 <= m)
                    q.add(new int[]{a, b, c});
            }
        }
        for (int i = 1; i <= n * m; i++) p[i] = i;
        while (sc.hasNext()) {
            int x1 = sc.nextInt(), y1 = sc.nextInt(), x2 = sc.nextInt(), y2 = sc.nextInt();
            int a = g[x1][y1], b = g[x2][y2], c = 0;
            q.add(new int[]{a, b, c});
        }
        int res = 0;
        while (!q.isEmpty()) {
            int[] t = q.poll();
            int a = find(t[0]), b = find(t[1]), c = t[2];
            if (a != b) {
                p[a] = b;
                res += c;
            }
        }
        out.println(res);
        out.flush();
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

